<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="类型" prop="gradeType">
        <el-select v-model="queryParams.gradeType" placeholder="请选择类型" clearable size="small">
          <el-option v-for="dict in dict.type.gradeType" :key="dict.value" :label="dict.label" :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="gradeName">
        <el-input v-model="queryParams.gradeName" placeholder="请输入名称(配置项)" clearable size="small"
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="isValid">
        <el-select v-model="queryParams.isValid" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in dict.type.Valid" :key="dict.value" :label="dict.label" :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"-->
    <!--          v-hasPermi="['pos:TGradeTypeInfo:add']">新增</el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-if="refreshTable" v-loading="loading" :data="TGradeTypeInfoList" row-key="id" default-expand-all
              :default-expand-all="isExpandAll" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="名称(配置项)" align="center" prop="gradeName" />
      <el-table-column label="vip" align="center" prop="gradeVipName" />
      <el-table-column label="类型" align="center" prop="gradeType">
        <template slot-scope="scope">
          <p v-if="scope.row.gradeType==0">目录</p>
          <p v-if="scope.row.gradeType==1">vip配置</p>
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="isValid">
        <template slot-scope="scope">
          <p v-if="scope.row.isValid==0">正常</p>
          <p v-if="scope.row.isValid==1">禁用</p>
          <p v-if="scope.row.isValid==2">删除</p>
        </template>
      </el-table-column>
      <el-table-column label="跨级分润百分百" align="center" prop="shareProfitBalance" />
      <el-table-column label="直属分润百分百" align="center" prop="shareProfit" />
      <el-table-column label="等额差之和" align="center" prop="badRatio" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <p v-if="scope.row.gradeType==0">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['pos:TGradeTypeInfo:edit']">修改</el-button>
          </p>
          <!--          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"-->
          <!--            v-hasPermi="['pos:TGradeTypeInfo:add']">新增</el-button>-->
          <!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['pos:TGradeTypeInfo:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改vip跨级分润，直营分润，类型配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="父ID" prop="parentId" v-if="false">
          <treeselect v-model="form.parentId" :options="TGradeTypeInfoOptions" :normalizer="normalizer"
                      placeholder="请选择父ID" />
        </el-form-item>
        <el-form-item label="名称" prop="gradeName">
          <el-input v-model="form.gradeName" placeholder="请输入名称" disabled/>
        </el-form-item>
        <el-form-item label="vip" prop="gradeId" v-if="false">
          <el-select v-model="form.gradeId" style="width: 100%;" clearable placeholder="请选择">
            <el-option v-for="item in GradeListOptions" :key="item.id" :label="item.gradeName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" v-if="false">
          <el-radio-group v-model="form.gradeType">
            <el-radio v-for="dict in dict.type.gradeType" :key="dict.value" :label="dict.value">{{dict.label}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="显示顺序" prop="sort">
          <el-input v-model="form.sort" type="number" placeholder="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="状态" prop="isValid" v-if="false">
          <el-radio-group v-model="form.isValid">
            <el-radio v-for="dict in dict.type.Valid" :key="dict.value" :label="dict.value">{{dict.label}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <!--<el-form-item label="跨级分润百分百" prop="shareProfitBalance">
          <el-input v-model="form.shareProfitBalance" placeholder="请输入跨级分润百分百" />
        </el-form-item>
        <el-form-item label="直属分润百分百" prop="shareProfit">
          <el-input v-model="form.shareProfit" placeholder="请输入直属分润百分百" />
        </el-form-item>-->
        <el-form-item label="等额差之和" prop="badRatio">
          <el-input v-model="form.badRatio" placeholder="请输入等额差之和" />
        </el-form-item>
        <el-table v-loading="loading" :data="childList">
          <el-table-column label="名称(配置项)" align="center" prop="gradeName" />
          <el-table-column label="vip" align="center" prop="gradeVipName" />
          <el-table-column label="类型" align="center" prop="gradeType">
            <template slot-scope="scope">
              <p v-if="scope.row.gradeType==0">目录</p>
              <p v-if="scope.row.gradeType==1">vip配置</p>
            </template>
          </el-table-column>
          <el-table-column label="显示顺序" align="center" prop="sort" />
          <el-table-column label="状态" align="center" prop="isValid">
            <template slot-scope="scope">
              <p v-if="scope.row.isValid==0">正常</p>
              <p v-if="scope.row.isValid==1">禁用</p>
              <p v-if="scope.row.isValid==2">删除</p>
            </template>
          </el-table-column>
          <el-table-column label="跨级分润百分百" align="center" prop="isValid">
            <template slot-scope="scope">
              <el-input v-model="scope.row.shareProfitBalance" placeholder="请输入跨级分润百分百" />
            </template>
          </el-table-column>
          <el-table-column label="直属分润百分百" align="center" prop="isValid">
            <template slot-scope="scope">
              <el-input v-model="scope.row.shareProfit" placeholder="请输入直属分润百分百" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listTGradeTypeInfo,
    getTGradeTypeInfo,
    delTGradeTypeInfo,
    addTGradeTypeInfo,
    updateTGradeTypeInfo,
    exportTGradeTypeInfo
  } from "@/api/pos/TGradeTypeInfo";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import {
    listTGradeInfo
  } from "@/api/pos/TGradeInfo";


  export default {
    name: "TGradeTypeInfo",
    dicts: ['gradeType', 'Valid'],
    components: {
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // vip跨级分润，直营分润，类型配置表格数据
        TGradeTypeInfoList: [],
        childList: [],
        // vip跨级分润，直营分润，类型配置树选项
        TGradeTypeInfoOptions: [],
        // vip
        GradeListOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否展开，默认全部折叠
        isExpandAll: false,
        // 重新渲染表格状态
        refreshTable: true,
        // 查询参数
        queryParams: {
          parentId: null,
          gradeId: "",
          gradeType: null,
          sort: null,
          gradeName: null,
          isValid: null,
          shareProfitBalance: null,
          shareProfit: null,
          badRatio: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      getGradeList() {
        listTGradeInfo(null).then(res => {
          this.GradeListOptions = res.rows
        })
      },
      /** 查询vip跨级分润，直营分润，类型配置列表 */
      getList() {
        this.loading = true;
        listTGradeTypeInfo(this.queryParams).then(response => {
          this.TGradeTypeInfoList = this.handleTree(response.data, "id");
          this.loading = false;
        });
      },
      /** 转换vip跨级分润，直营分润，类型配置数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        };
      },
      /** 查询vip跨级分润，直营分润，类型配置下拉树结构 */
      getTreeselect() {
        listTGradeTypeInfo({gradeType:0}).then(response => {
          this.TGradeTypeInfoOptions = [];
          let data = {
            id: 0,
            name: '顶级节点',
            children: []
          };
          let result = response.data;
          let children = result.map(item => {
            let obj = {
              id:item.id,
              name:item.gradeName,
              children: []
            };
            return obj;
          })
          data.children = this.handleTree(children, "id");
          console.log(data)
          this.TGradeTypeInfoOptions.push(data);
        });
      },

      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          parentId: 0,
          gradeId: null,
          gradeType: "0",
          sort: null,
          gradeName: null,
          isValid: "0",
          shareProfitBalance: null,
          shareProfit: null,
          badRatio: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.getGradeList();
        this.reset();
        this.getTreeselect();
        if (row != null && row.id) {
          this.form.parentId = row.id;
        } else {
          this.form.parentId = 0;
        }
        this.open = true;
        this.title = "添加vip跨级分润，直营分润，类型配置";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.getGradeList();
        this.reset();
        this.getTreeselect();
        if (row != null) {
          this.form.parentId = row.id;
        }
        //this.loading = true;
        getTGradeTypeInfo(row.id).then(response => {
          this.form = response.data;
          this.open = true;
          this.childList = response.data.childList;
          this.loading = false;
          this.title = "跨级分润配置";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateTGradeTypeInfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addTGradeTypeInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除？').then(function() {
          return delTGradeTypeInfo(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },

      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
    }
  };
</script>
